﻿using FileStorageCenter.Services.Models;
using Microsoft.EntityFrameworkCore;

namespace FileStorageCenter.Services.Services.Impl;

public class UserService : IUserService
{
    private readonly DbContext _context;

    public UserService(DbContext context)
    {
        _context = context;
    }

    public async Task<ListResult<User.QueryModel>> GetListAsync(CancellationToken cancellationToken)
    {
        var result = new ListResult<User.QueryModel>();

        try
        {
            var users = await _context.Set<User>()
                .AsNoTracking()
                .ToListAsync(cancellationToken);

            result.Data = users.Select(x => GetQueryModel(x));

            result.Status = OperationalResult.SUCCESS;
        }
        catch (Exception error)
        {
            result.Status = OperationalResult.ERROR;

            result.Message = error.ToString();
        }

        return result;
    }

    public static User.QueryModel GetQueryModel(User user)
    {
        return new User.QueryModel
        {
            Id = user.Id,
            CreatedDate = user.CreatedDate,
            LastModifiedDate = user.LastModifiedDate,
            Account = user.Account,
            Name = user.Name,
        };
    }
}
